using Ns.Threading;

namespace Ns.Data.Entity
{
    /// <summary>
    /// <para>Overrides the default <see cref="ChangeTracking"/> within a code block.</para>
    /// <para>This scope should be considered when fetching entities from a data store 
    /// since by default the <see cref="ChangeTracking"/> is <see cref="ChangeTracking.Enabled"/> 
    /// (meaning the <see cref="PersistChange"/> is set to <see cref="PersistChange.Insert"/> 
    /// when instanciating an entity).</para></summary>
    /// <example>
    /// In the example below the <see cref="ChangeTrackingScope"/> leaves 
    /// the <see cref="IPersistEntity.PersistChange"/> of each entity instanciated within the scope
    /// to <see cref="PersistChange.None"/> since the <see cref="ChangeTracking"/> is 
    /// <see cref="ChangeTracking.Disabled"/>.
    /// <code>
    /// EntitySet&lt;Company&gt; GetCompanies(DateTime establishedOn, Company graphCompany)
    /// {            
    ///       using (new ChangeTrackingScope(ChangeTracking.Disabled))
    ///       {
    ///         var companies = GetCompanies(establishedOn);
    ///         
    ///         if (graphCompany != null &amp;&amp;
    ///             graphCompany.Workers.Count != 0)
    ///             { 
    ///                 foreach (var company in companies)
    ///                 {
    ///                     company.Workers = GetCompanyWorkers(company, graphCompany.Workers[0]);
    ///                 }
    ///             }
    ///             
    ///         return companies;
    ///       }
    /// }
    /// </code>
    /// </example>
    public sealed class ChangeTrackingScope : Scope<ChangeTracking?>
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="ChangeTrackingScope"/> class.
        /// </summary>
        /// <param name="changeTracking">The change tracking.</param>
        public ChangeTrackingScope(ChangeTracking changeTracking)
            : base(changeTracking)
        {
        }

        /// <summary>
        /// Prevents a default instance of the <see cref="ChangeTrackingScope"/> class from being created.
        /// </summary>
        private ChangeTrackingScope()
        {

        }
    }
}